﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.IRepository.SystemSettings.QualificationAdministration;
using SystemCenterSoa.PoModel.SystemSettings.QualificationAdministration;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemSettings.QualificationAdministration
{
    public class DictionaryTypeTableRepository : BaseRepository, IDictionaryTypeTableRepository
    {
        public DictionaryTypeTableRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {

        }

        public bool DictionaryTypeTableAdd(DictionaryTypeTablePo info)
        {
            _systemCenterDbSoaContext.DictionaryTypeTablePo.Add(info);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public bool DictionaryTypeTableDelete(string dttypekey)
        {
            var list = _systemCenterDbSoaContext.DictionaryTypeTablePo.Find(dttypekey);
            _systemCenterDbSoaContext.DictionaryTypeTablePo.Remove(list);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public DictionaryTypeTablePo DictionaryTypeTableEdit(string dttypekey)
        {
            return _systemCenterDbSoaContext.DictionaryTypeTablePo.FirstOrDefault(p => p.dt_type_key.Equals(dttypekey));
        }

        public PagerResult<DictionaryTypeTablePo> SearchPage(SearchModel searchModel)
        {
            var query = _systemCenterDbSoaContext.DictionaryTypeTablePo.AsQueryable();
            //判断有没有搜索条件
            if (searchModel.SearchItem != null && searchModel.SearchItem.Count > 0)
            {
                //把搜索条件拿出来
                var tempDi = searchModel.SearchItem;

                //搜索字段为role_name
                string name = "name";
                //判断搜索条件中是否有role_name
                if (tempDi.Keys.Contains(name) && !string.IsNullOrEmpty(tempDi[name]))
                {
                    query = query.Where(c => c.dt_type_name.Contains(tempDi[name]));
                }

            }
            return BasePageSearch(query, searchModel);
        }

        public bool DictionaryTypeTableUpdate(DictionaryTypeTablePo info)
        {
            _systemCenterDbSoaContext.Entry(info).State = EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }
    }
}
